package com.roubsite.admin.dao.rsSystemLog;

import java.sql.Types;
import java.util.List;

import com.roubsite.admin.bean.RsSystemLog;
import com.roubsite.database.bean.Record;
import com.roubsite.database.dao.DataSet;
import com.roubsite.database.dao.EntityDao;
import com.roubsite.utils.StringUtils;
import com.roubsite.utils.UuidUtils;

public class RsSystemLogDao extends EntityDao implements IRsSystemLogDao {

	@Override
	public DataSet getInfoById(String id) {
		DataSet ds = this.queryBean("SELECT * FROM RS_SYSTEM_LOG WHERE ID =?", new String[] { id },
				new int[] { Types.VARCHAR }, false, 0, 0, RsSystemLog.class);
		return ds;
	}

	@Override
	public DataSet queryd(Record record, int start, int rows) {
		StringBuffer sql = new StringBuffer("SELECT * FROM RS_SYSTEM_LOG WHERE 1=1 ");
		RsSystemLog dataBean = (RsSystemLog) record.toBean(RsSystemLog.class);
		if (StringUtils.isNotEmpty(dataBean.getUserId())) {
			sql.append(" AND USER_ID LIKE '%");
			sql.append(dataBean.getUserId());
			sql.append("%'");
		}
		sql.append(" ORDER BY ID DESC");
		return this.queryBean(sql.toString(), new String[] {}, null, true, start, rows, RsSystemLog.class);
	}

	@Override
	public int saveData(Record record, Class<?> bean) {
		return this.save(record, bean);
	}

	@Override
	public int del(List ids) throws Exception {
		// TODO Auto-generated method stub
		String tableName = "RS_SYSTEM_LOG";
		String keyField = "ID";
		StringBuffer delSql = new StringBuffer("DELETE FROM " + tableName + " WHERE " + keyField + " IN (");
		for (int i = 0; i < ids.size(); i++) {
			delSql.append("'");
			delSql.append(ids.get(i));
			delSql.append("',");
		}
		String sql = delSql.substring(0, delSql.length() - 1);
		sql = sql + ")";

		return this.excute(sql, new Object[] {}, new int[] {});
	}

	@Override
	public void insertLog(String ip, String userId, String describe, String page, String uri, String ua) {
		String sql = "INSERT INTO RS_SYSTEM_LOG(ID, USER_ID, IP, PAGE, URI, LOG_DESCRIBE, UA, CREATE_TIME) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
		this.excute(sql,
				new String[] { UuidUtils.getUuid(), userId, ip, page, uri, describe, ua, StringUtils.getUnixDate() },
				new int[] { Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
						Types.VARCHAR, Types.VARCHAR });
	}

}
